package com.chuanyang.driverh.utils.net;

import android.content.Context;
import android.util.Log;

import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.eventbus.ErrorEvent;
import com.zhy.http.okhttp.callback.GenericsCallback;
import com.zhy.http.okhttp.callback.IGenericsSerializator;

import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


/**
 * Created by xupeixian on 2017/2/12.
 */

public abstract class MyCallback<T extends Result> extends GenericsCallback<T> {

    private Context context;
    public MyCallback(Context context,IGenericsSerializator serializator) {
        super(serializator);
        this.context=context;
    }

    @Override
    public T parseNetworkResponse(Response response, int id) {
        try {
            return super.parseNetworkResponse(response, id);
        } catch (Exception e) {
            EventBus.getDefault().post( new ErrorEvent(ConstUtil.HTTP_PARSER_ERROR,"解析错误", context, context));
            onError(null,e,id);
            return null;
        }
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        DialogUtil.cancleLoadingDialog();
        onFail(call,e,id);
    }

    @Override
    public void onResponse(T response, int id) {
        DialogUtil.cancleLoadingDialog();
        if (response==null){
            return;
        }
        onSuccess(response,id);
    }

    public abstract void onFail(Call call, Exception e, int id);

    public abstract void onSuccess(T response, int id);




}
